package com.dau.system.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dau.common.annotation.CurrentUser;
import com.dau.common.page.PageQuery;
import com.dau.common.page.TableDataInfo;
import com.dau.common.result.R;
import com.dau.entity.system.dept.DeptAndUser;
import com.dau.entity.system.dept.SysDept;
import com.dau.entity.system.user.SysUser;
import com.dau.service.system.dept.SysDeptService;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统部门控制器
 *
 * @author tzj
 * @date 2024/11/20
 */
@RestController
@RequestMapping("sys-dept")
public class SysDeptController {
    @Resource
    private SysDeptService sysDeptService;

    /**
     * 部门信息
     *
     * @param id id
     * @return {@link R }<{@link Boolean }>
     */
    @GetMapping("/{id}")
    @RequiresPermissions("dept:info:query")
    public R<SysDept> info(@PathVariable String id) {
        return R.success(sysDeptService.queryDeptInfo(id));
    }

    /**
     * 部门分页数据
     *
     * @param pageQuery 分页参数
     * @param sysDept   系统部门
     * @return {@link R }<{@link IPage }<{@link SysUser }>>
     */
    @GetMapping("/page")
    @RequiresPermissions("dept:info:query")
    public TableDataInfo<SysDept> selectUserPage(SysDept sysDept, PageQuery pageQuery) {
        return (sysDeptService.selectDeptPage(sysDept, pageQuery));
    }

    /**
     * 添加或更新
     *
     * @param sysDept     系统部门
     * @param currentUser 用户
     * @return {@link R }<{@link Boolean }>
     */
    @PostMapping("/addOrUpdate")
    @RequiresPermissions("dept:info:update")
    public R<?> addOrUpdate(@RequestBody @Validated SysDept sysDept, @CurrentUser SysUser currentUser) {
        sysDeptService.addOrUpdate(sysDept, currentUser.getId());
        return R.success();
    }


    /**
     * 删除
     *
     * @param id id
     * @return {@link R }<{@link Boolean }>
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("dept:info:delete")
    public R<Boolean> delete(@PathVariable String id) {
        return R.success(sysDeptService.removeById(id));
    }


    /**
     * 绑定部门和用户
     *
     * @param deptAndUser 部门和用户
     * @return {@link R }<{@link Boolean }>
     */
    @PostMapping("/bind")
    @RequiresPermissions("dept:info:update")
    public R<?> bind(@RequestBody @Validated DeptAndUser deptAndUser) {
        sysDeptService.bind(deptAndUser);
        return R.success();
    }
}
